My Flexdashboard

data("ny_noaa")

noaa <- ny_noaa |>
  clean_names() |>
  mutate(date = as.Date(date),
         year = year(date),
         month = month(date, label = TRUE, abbr = TRUE, locale = "C"),
         tmax_c = as.numeric(tmax)/10,
         tmin_c = as.numeric(tmin)/10,
         prcp_mm = as.numeric(prcp)) |>
  drop_na(date)
top_station <- noaa |> count(id, sort = TRUE) |> slice(1) |> pull(id)
noaa_s <- noaa |> filter(id == top_station) |> slice_sample(n = 40000)

Column

Chart A

renderPlotly({
  df <- noaa_s |>
    filter(date >= input$daterng[1], date <= input$daterng[2]) |>
    select(date, tmax_c, tmin_c) |>
    pivot_longer(c(tmax_c, tmin_c), names_to = "series", values_to = "value") |>
    drop_na(value)

  p <- ggplot(df, aes(date, value, color = series)) +
    geom_line(alpha = .7) +
    labs(x = NULL, y = "°C", color = NULL)
  if (isTRUE(input$smooth)) p <- p + geom_smooth(se = FALSE, span = .2, method = "loess")
  ggplotly(p, tooltip = c("x","y","color")) |> layout(legend = list(orientation = "h"))
})

Column

Chart B

renderPlotly({
  df <- noaa_s |>
    filter(date >= input$daterng[1], date <= input$daterng[2]) |>
    drop_na(tmax_c, tmin_c)

  ggplotly(
    ggplot(df, aes(x = month, y = tmax_c)) +
      geom_boxplot(fill = "#3182bd", color = "white", alpha = 0.7, outlier.alpha = 0.3) +
      labs(
        x = "Month",
        y = "Tmax (°C)",
        title = "Monthly Distribution of Maximum Temperature"
      )
  )
})

Chart C

renderPlotly({
  df <- noaa_s |>
    filter(date >= input$daterng[1], date <= input$daterng[2]) |>
    mutate(wet_day = !is.na(prcp_mm) & prcp_mm > 0) |>
    count(year, month, wet_day, name = "n") |>
    filter(wet_day) |>
    group_by(month) |>
    summarise(wet_days = sum(n), .groups = "drop")

  ggplotly(
    ggplot(df, aes(month, wet_days)) +
      geom_col() +
      labs(x = NULL, y = "Wet days")
  )
})